﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.Consts;

namespace BySoft.Server.DataAccess
{
    //modify by Pater at 2009-09-10
    public partial class SysParametertypeDA
    {

        /// <summary>
        ///  更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int Update(SysParametertypeEntity entity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                #region SQL Server
                string strSQL = "update " + entity.TableSchema.TableName + " set ";
                strSQL += SysParametertypeTable.C_PARAMETERTYPEID + "=@ID";
                strSQL += ", " + SysParametertypeTable.C_PARAMETERTYPENAME + "=@" + SysParametertypeTable.C_PARAMETERTYPENAME;
                strSQL += ", " + SysParametertypeTable.C_STATUS + "=@" + SysParametertypeTable.C_STATUS;
                strSQL += ", " + SysParametertypeTable.C_MODIFYDATE + "=@" + SysParametertypeTable.C_MODIFYDATE;
                strSQL += ", " + SysParametertypeTable.C_MODIFUUSERID + "=@" + SysParametertypeTable.C_MODIFUUSERID;
                strSQL += ", " + SysParametertypeTable.C_ISSOLIDIFY + "=@" + SysParametertypeTable.C_ISSOLIDIFY;
                strSQL += ", " + SysParametertypeTable.C_DESCRIPTION + "=@" + SysParametertypeTable.C_DESCRIPTION;
                strSQL += " where upper(" + SysParametertypeTable.C_PARAMETERTYPEID + ")=@" + SysParametertypeTable.C_PARAMETERTYPEID;
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@" + SysParametertypeTable.C_ISSOLIDIFY, entity.IsSolidify);
                dpc.AddWithValue("@" + SysParametertypeTable.C_MODIFYDATE, entity.ModifyDate);
                dpc.AddWithValue("@" + SysParametertypeTable.C_MODIFUUSERID, entity.ModifuUserID);
                dpc.AddWithValue("@" + SysParametertypeTable.C_PARAMETERTYPENAME, entity.ParameterTypeName);
                dpc.AddWithValue("@" + SysParametertypeTable.C_PARAMETERTYPEID, entity.ParameterTypeID.ToUpper());
                dpc.AddWithValue("@" + SysParametertypeTable.C_DESCRIPTION, entity.Description);
                dpc.AddWithValue("@" + SysParametertypeTable.C_STATUS, entity.Status);
                dpc.AddWithValue("@ID", entity.ParameterTypeID);
                return broker.ExecuteSQL(strSQL, dpc);
                #endregion
            }
        }

        /// <summary>
        ///   根据ID更新状态
        /// </summary>
        /// <param name="strID">参数类别ID</param>
        /// <param name="status">Status</param>
        /// <returns></returns>
        public static int UpdateSatus(string strID, Common.Enum.BaseEnum.CommonSatus status)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                #region SQL Server
                string strSQL = "update " + SysParametertypeTable.C_TABLENAME + " set ";

                strSQL += SysParametertypeTable.C_STATUS + "=@" + SysParametertypeTable.C_STATUS;

                strSQL += " where " + SysParametertypeTable.C_PARAMETERTYPEID + "=@" + SysParametertypeTable.C_PARAMETERTYPEID;

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@" + SysParametertypeTable.C_PARAMETERTYPEID, strID);
                dpc.AddWithValue("@" + SysParametertypeTable.C_STATUS, ((int)status).ToString());

                return broker.ExecuteSQL(strSQL, dpc);
                #endregion
            }
        }

        #region GetAll
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>所有数据集合</returns>
        public static DataSet GetAll()
        {
            //string strSQL = "SELECT * FROM " + SysParametertypeTable.C_TABLENAME;
            string strSQL = @"select t.*,u.username as CreatorName,p.username as ModifyUserName from "
                + SysParametertypeTable.C_TABLENAME + " t left join "
                + SysUserTable.C_TABLENAME + " u on t." + SysParametertypeTable.C_CREATEUSERID + "=u."
                + SysUserTable.C_USERID + " left join " + SysUserTable.C_TABLENAME + " p on p."
                + SysUserTable.C_USERID + "=t." + SysParametertypeTable.C_CREATEUSERID;
            return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(strSQL);
        }
        #endregion

        #region GetByID
        /// <summary>
        ///  根据类别ID获得类别实体，类别ID不区分大小写
        /// </summary>
        /// <returns></returns>
        public static SysParametertypeEntity GetByID(string strID)
        {
             #region SQL Server
                string strSql = string.Format("select * from {0} where upper({1})=@{1}",
                    SysParametertypeTable.C_TABLENAME,
                    SysParametertypeTable.C_PARAMETERTYPEID,
                    SysParametertypeTable.C_PARAMETERTYPEID);
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@" + SysParametertypeTable.C_PARAMETERTYPEID, strID.ToUpper());
                return DataAccessCommon.SelectSingle<SysParametertypeEntity>(strSql, dpc);
                #endregion
        }
        #endregion
    }
}
